package com.wolfpire.subject.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.profession.dao.ProfessionDao;
import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.subject.dao.SubjectDao;
import com.wolfpire.subject.dao.UserGroupSubjectRelationDao;
import com.wolfpire.subject.service.SubjectService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.MD5;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.dao.AuthorityDao;
import com.wolfpire.system.dao.OrgDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.dao.RoleDao;
import com.wolfpire.system.dao.UserDao;
import com.wolfpire.system.dao.UserRoleRelationDao;
import com.wolfpire.system.model.Attachment;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.User;
import com.wolfpire.system.model.UserRoleRelation;
import com.wolfpire.system.service.AttachmentService;
import com.wolfpire.system.service.UserService;

@Service("subjectService")
public class SubjectServiceImpl extends BaseHibernateService<Subject, Long> implements
	SubjectService {
	
	@Resource private SubjectDao subjectDao;
	@Resource private ProfessionDao professionDao;
	@Resource private ProfessionSubjectRelationDao professionSubjectRelationDao;
	@Resource private AttachmentService attachmentService;
	@Resource private UserGroupSubjectRelationDao userGroupSubjectRelationDao;

	public List<Subject> list(Subject subject) {
		// TODO Auto-generated method stub
		return this.subjectDao.list(subject);
	}

	@Override
	protected IBaseHibernateDao<Subject, Long> getEntityDao() {
		// TODO Auto-generated method stub
		return this.subjectDao;
	}

	public Subject getByName(String name) {
		// TODO Auto-generated method stub
		return this.subjectDao.getByName(name);
	}

	public Page<Subject> setPageDataList(Page<Subject> page,
			Subject filterSubject) {
		// TODO Auto-generated method stub
		return this.subjectDao.findSubjects(page, filterSubject);
	}

	public List<Subject> getSubjectsByProfessionId(Long professionId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void saveOrUpdate(Subject subject) {
		//先保存相关附件
		this.attachmentService.batchRelateAttachment(subject.getAttId(), subject.getId(), "subject");
		//查找出对应的附件
		List<Attachment> attachments = this.attachmentService.getByBiz(subject.getId(), "subject");
		
		if(attachments != null && attachments.size() > 0){
			String filePath = attachments.get(0).getFilePath();
			subject.setImage(filePath);
		}
		if (null == subject.getId()) {
			this.subjectDao.saveOrUpdate(subject);
		} else {
			this.subjectDao.merge(subject);
		} 
		
		Long subjectId = subject.getId();
		Long professionId = subject.getProfessionId();
		this.professionSubjectRelationDao.relate(professionId, subjectId);
	}

	public Subject getCompleteSubject(Long id) {
		Subject subject = this.get(id);
		// 绑定专业
		ProfessionSubjectRelation ps = this.professionSubjectRelationDao.getPSRelation(id);
		subject.setProfessionId(ps.getProfessionId());
		Profession profession = this.professionDao.get(ps.getProfessionId());
		/*if (null != profession) {
			subject.setName(profession.getName());
		}*/
		return subject;
	}

	public List<Map<String, Object>> bulidTree(Subject subject) {
		// TODO Auto-generated method stub
		// 获取专业list
		Profession profession = new Profession();
		List<Profession> pList = this.professionDao.list(profession );
		List<Map<String, Object>> subjects = this.subjectDao.querySubjectFullInfo();
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		for (Profession p : pList) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", p.getId());
			map.put("text", p.getName());
			map.put("nodeType", "profession");
			map.put("state", "open");
			List<Map<String, Object>> children = new ArrayList<Map<String,Object>>();
			for (Map<String,Object> sub : subjects) {
				if (p.getId().toString().equals(sub.get("profession_id").toString())) {
					Map<String, Object> subMap = new HashMap<String, Object>();
					subMap.put("id", sub.get("id"));
					subMap.put("text", sub.get("name"));
					subMap.put("state", "open");
					subMap.put("nodeType", "subject");
					children.add(subMap);
				}
			}
			map.put("children", children);
			lists.add(map);
		}
		return lists;
	}

	public List<Subject> list(int num) {
		// TODO Auto-generated method stub
		return this.subjectDao.list(num);
	}

	public Subject getById(long id) {
		// TODO Auto-generated method stub
		return this.subjectDao.getById(id);
	}

	public void relate(Long usergroupId, Long subjectId) {
		// TODO Auto-generated method stub
		this.userGroupSubjectRelationDao.relate(usergroupId, subjectId);
	}

	public void saveUserGroupSubject(Long id, String subjectIdsStr) {
		List<Long> subjectIds = new ArrayList<Long>();
		if (!StringUtils.isBlank(subjectIdsStr)) {
			String[] subjectIdsArr = subjectIdsStr.split(",");
			for (String str : subjectIdsArr) {
				subjectIds.add(Long.parseLong(str));
			}
		}
		this.userGroupSubjectRelationDao.relate(id, subjectIds);
		
	}

	public List<Subject> findSubjectByUserGroupId(long userGroupId) {
		return this.subjectDao.findSubjectByUserGroupId(userGroupId);
	}

	public List<Subject> findAllSubject() {
		// TODO Auto-generated method stub
		return this.subjectDao.findAllSubject();
	}

	public Page<Map<String, Object>> findSubjects(Page<Map<String, Object>> page, Subject filterSubject,
			long usergrouoId) {
		// TODO Auto-generated method stub
		return this.subjectDao.findSubjects(page, filterSubject, usergrouoId);
	}
	
	
}
